<?php
/**
 *  Controller_Modify: modify a model (item) by one property at a time
 *
 *  Use POST method
 *  Parameters:
 *  - GET: item id
 *  - POST: propertyName, propertyValue
 */
namespace Yz\modules\Generic;


class Controller_Modify
    extends \Yz_Controller_Abstract
{


    /** controls whether controller appears in `actions menu`
     *  @var bool
     */
    const IN_MENU = false;

#    const ACCESS = \Yz_Access_Interface::AC_VALID_USER;

    protected $viewClass = 'Yz_View_JSON';

    /**
     *
     */
    protected function xxxxdoPOST()
    {
        // 1. arguments
        $id = $this->fetchArgument(0);

        $rq = $this->getRequest();

        $propName = $rq->fetchPOSTArgument("propertyName", "");
        if ("" === $propName) {
            throw new \Yz_Exception("No property name specified");
        }
        $propValue = $rq->fetchPOSTArgument("propertyValue");

        // 2. logic
        $model = $this->module->factory("Model_Default");

        $item = $model->fetchItemById($id);

        $item[$propName] = $propValue;

        $model->updateItemById($item);

        // 3. output
        $this->getView()->setJSON($item);

        return $this;
    }

}
